package game.resources;

import game.graphics.Animation;
import game.graphics.Frame;
import game.graphics.LoopbackAnimation;
import game.structures.Point;

import java.util.ArrayList;
import java.util.HashMap;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
//import org.jdom.output.XMLOutputter;
import java.io.File;
import java.util.List;

import org.newdawn.slick.Image;

public class ResourceManager {

	private static ResourceManager instance = null;
	
	private HashMap<String, Image> imageMap;
	private HashMap<String, Animation> animationMap;
	
	private ArrayList<String> loadedImageLists;
	private ArrayList<String> loadedAnimationLists;
	
	private ResourceManager() {
		imageMap = new HashMap<String, Image>();
		animationMap = new HashMap<String, Animation>();
		
		loadedImageLists = 		new ArrayList<String>();
		loadedAnimationLists = 	new ArrayList<String>();
	}
	
	public static ResourceManager getInstance() {
		if (instance == null)
			instance = new ResourceManager();
		return instance;
	}
	
	public void loadImages(String filename) {
		try {
			File xmlFile = new File(filename);
			SAXBuilder builder = new SAXBuilder();
			Document doc = builder.build(xmlFile);
			Element element = doc.getRootElement();
			
			String name = element.getAttributeValue("name");
			if (imageMap.containsKey(name)) {
				System.out.println("Attempted to add duplicate key");
				return;
			}
			
			loadedImageLists.add(name);
			List list = element.getChildren();
			for (int x = 0; x < list.size(); x++) {
				Element e = (Element)list.get(x);
				String key = e.getAttributeValue("name");
				
				if(!imageMap.containsKey(key))
					imageMap.put(key, new Image(e.getAttributeValue("file")));
			}
		} catch (Exception e) {
			System.out.println(e);
		}
	}
	
	public void loadLayer(String filename) {
		
	}
	
	public void loadAnimation(String filename) {
		try {
			File xmlFile = new File(filename);
			SAXBuilder builder = new SAXBuilder();
			Document doc = builder.build(xmlFile);
			Element element = doc.getRootElement();
			
			String listName = element.getAttributeValue("name");
			if (loadedAnimationLists.contains(listName)) {
				System.out.println("Attempted to add animation list that is already loaded");
				return;
			}
			
			String image = element.getAttributeValue("image");
			if (!imageMap.containsKey(image)) {
				System.out.println("Attempted to use image without loading it first");
				return;
			}
			
			List animations = element.getChildren();
			
			
			for (int y = 0; y < animations.size(); y++) {
				
				List list = ((Element) animations.get(y)).getChildren();
				ArrayList<Frame> frames = new ArrayList<Frame>();
				
				for (int x = 0; x < list.size(); x++) {
					
					Element e = (Element)list.get(x);
					frames.add(new Frame(
							(Image)imageMap.get(image),
							new Point(
									e.getAttributeValue("x1"),
									e.getAttributeValue("y1")),
							new Point(
									e.getAttributeValue("x2"),
									e.getAttributeValue("y2")),
							new Point(
									e.getAttributeValue("o1"),
									e.getAttributeValue("o2")),
							Integer.parseInt(e.getAttributeValue("end")),
							Integer.parseInt(e.getAttributeValue("next"))
							));
				}
				
				animationMap.put(	((Element)animations.get(y)).getAttributeValue("name"), 
									new LoopbackAnimation(
												((Element)animations.get(y)).getAttributeValue("name"),
												frames, 
												Integer.parseInt(((Element)animations.get(y)).getAttributeValue("exitFrame"))));
			}
			
		} catch (Exception e) {
			System.out.println(e);
		}
	}
	
	public void loadTileList(String filename) {
		
	}
	
	public void loadObjectMap(String filename) {
		
	}
	
	public boolean imageExists(String key) {
		return imageMap.containsKey(key);
	}
	
	public boolean animationExists(String key) {
		return animationMap.containsKey(key);
	}
	
	public Image getImage(String key) {
		return (Image)imageMap.get(key);
	}
	
	public Animation getAnimation(String key) {
		return (Animation)animationMap.get(key);
	}
	
	public boolean removeImage(String key) {
		Object o = imageMap.remove(key);
		if (o != null)
			return true;
		return false;
	}
	
	public boolean removeAnimation(String key) {
		Object o = animationMap.remove(key);
		if (o != null)
			return true;
		return false;
	}
}
